package lily.com.app;

import java.util.ArrayList;

import lily.com.app.TaskDao.TaskDaoFields;
import lily.com.app.TaskDomain.TaskStatus;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class TaskListViewAdapter extends BaseAdapter {

	private ArrayList<TaskDomain> dataList = null;
	private LayoutInflater inflator = null;
	private Context context = null;
	private int colorInterval;
	private TaskDao taskDao = null;
	private static TaskListViewAdapter thisInstance = null;

	private TaskListViewAdapter(Context context) {
		this.context = context;
		taskDao = new TaskDao();
		inflator = LayoutInflater.from(context);
//		getData();
	}
	
	public static TaskListViewAdapter getInstance(Context context){
		if(null == thisInstance){
			thisInstance = new TaskListViewAdapter(context);
		}
		return thisInstance;
	}

	@Override
	public int getCount() {
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		if(null == dataList)
			return null;
		else
			return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public ArrayList<TaskDomain> getDataList() {
		return dataList;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (0 == position) {
			colorInterval = 0xFF / dataList.size();
		}
		
		convertView = inflator.inflate(R.layout.list_item, null);
		TaskDomain taskDomain = dataList.get(position);
		int status = taskDomain.getStatus();
		int color = ((colorInterval * position) << 8) | Color.RED;

		TextView txtRecord = (TextView) convertView
				.findViewById(R.id.txtRecord);
		if(status == TaskDomain.TaskStatus.NEW){
			txtRecord.setBackgroundColor(0xFF33B5E5);
			txtRecord.setText(taskDomain.getTitle());
		}
		else if (status == TaskDomain.TaskStatus.DONE) {
			txtRecord.setBackgroundColor(color);
			String title = taskDomain.getTitle();
			SpannableString ss = new SpannableString(title);
			ss.setSpan(new StrikethroughSpan(), 0, title.length(),
					Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			txtRecord.setText(ss);
		} else {
			txtRecord.setBackgroundColor(color);
			txtRecord.setText(taskDomain.getTitle());
		}
		
		//TODO modify
		ListItemOnClickListener listItemOnClickListener = new ListItemOnClickListener();
		txtRecord.setOnClickListener(listItemOnClickListener);
		
		Button btnFinish = (Button) convertView.findViewById(R.id.btnFinish);
		btnFinish.setOnClickListener(listItemOnClickListener);

		Button btnDelete = (Button) convertView.findViewById(R.id.btnDelete);
		btnDelete.setOnClickListener(listItemOnClickListener);
		
		convertView.setLongClickable(true);
		convertView.setId(taskDomain.getTid());
		
		return convertView;
	}
	
//	public Context getContext(){
//		return context;
//	}
	
	public void insertItem(int postion){
		if(null != dataList){
			TaskDomain newTask = new TaskDomain();
			newTask.setTid(-1);
			newTask.setTitle("新建任务");
			newTask.setText("在此输入任务内容。。。");
			newTask.setStatus(TaskStatus.NEW);
			dataList.add(postion, newTask);
			notifyDataSetChanged();
		}
	}
	
	public void insertTask(String title, String text, int status){
		insertTask(0, title, text, status);
	}
	
	public void insertTask(int postion, String title, String text, int status){
		int tid = (int)taskDao.insertTask(title, text, status);
		if(tid < 0){
			Log.d("DEBUG", "TaskListViewAdapter insertTask fail");
			return;
		}
		TaskDomain task = new TaskDomain();
		task.setTid(tid);
		task.setText(text);
		task.setTitle(title);
		task.setStatus(status);
		dataList.add(postion, task);
		notifyDataSetChanged();
	}
	
	public void flushRowsIntoDB(){
//		Log.d("DEBUG", "flushRowsIntoDB");
		taskDao.dropTableOrders();
		taskDao.createTableOrders();
		//TODO for debugger, to delete
//		for (int i = 0; i < dataList.size(); ++i)
//			Log.d("DEBUG", dataList.get(i).toString());
		taskDao.insertAllOrders(dataList);
	}
	
	//TODO is return value needed
	public ArrayList<TaskDomain> selectAllTask() {
//		Log.d("DEBUG", "selectAllTask()");
		Cursor cursor = taskDao.selectAllOrders();
		TaskDomain task = null;
		dataList = new ArrayList<TaskDomain>();
		int tid = -1;
		while (cursor.moveToNext()) {
			tid = cursor.getInt(cursor.getColumnIndex(TaskDaoFields.COL_NAME_TID));
//			Log.d("DEBUG", "cursor tid = " + tid);
			task = taskDao.selectTask(tid);
//			if(null == task)
//				Log.d("DEBUG", "null == task");
			dataList .add(task);
		}
		notifyDataSetChanged();
		
//		Log.d("DEBUG", "!!!");
		//TODO for debugger, to delete
//		for(int i = 0; i < dataList.size(); ++i){
//			if(null == dataList.get(i)){
//				Log.d("DEBUG", "datalist[" + i + "] is null");
//			}else{
//				Log.d("DEBUG", dataList.get(i).toString());
//			}
//		}
		
		return dataList;
		
	}
	
	//TODO check is there error
	public void moveTask(int from, int to){
//		Log.d("DEBUG", "moveTask");
		TaskDomain fromTask = dataList.get(from);
		TaskDomain toTask = dataList.get(to);
		dataList.set(to, fromTask);
		dataList.set(from, toTask);
		notifyDataSetChanged();
	}
	
	public void deleteTask(int tid){
//		Log.d("DEBUG", "TaskListViewAdapter deleteTask()");
		taskDao.deleteTask(tid);
		taskDao.deleteOrder(tid);
		deleteItem(tid);
		notifyDataSetChanged();
	}
	
	public void updateTask(int tid, String title, String text, int status){
		taskDao.updateTask(tid, title, text, status);
		notifyDataSetChanged();
	}
	
	public void deleteItem(int tid) {
		TaskDomain task = null;
		for(int i = 0 ; i < dataList.size() ; i++){
			task = dataList.get(i);
			if(tid == task.getTid()){
				dataList.remove(i);
				break;
			}
		}
	}
	
	public void updateTaskStatus(int tid, int status, int uiStatus){
		taskDao.updateTaskStatus(tid, status);
		updateItem(tid, status, uiStatus);
		notifyDataSetChanged();
	}
	
	public void updateTaskStatus(int tid, int status){
		taskDao.updateTaskStatus(tid, status);
		updateItem(tid, status);
		notifyDataSetChanged();
	}
	
	public void updateItem(int tid, int status) {
		TaskDomain task = null;
		for(int i = 0 ; i < dataList.size() ; i++){
			task = dataList.get(i);
			if(tid == task.getTid())
				task.setStatus(status);
		}
	}
	
	public void updateItem(int tid, int status, int uiStatus) {
		TaskDomain task = null;
		for(int i = 0 ; i < dataList.size() ; i++){
			task = dataList.get(i);
			if(tid == task.getTid()){
				task.setStatus(status);
				task.setUiStatus(uiStatus);
			}
		}
	}
}
